<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;


//Amharic	am
//Arabic	ar
//Basque	eu
//Bengali	bn
//English (UK)	en-GB
//Portuguese (Brazil)	pt-BR
//Bulgarian	bg
//Catalan	ca
//Cherokee	chr
//Croatian	hr
//Czech	cs
//Danish	da
//Dutch	nl
//English (US)	en
//Estonian	et
//Filipino	fil
//Finnish	fi
//French	fr
//German	de
//Greek	el
//Gujarati	gu
//Hebrew	iw
//Hindi	hi
//Hungarian	hu
//Icelandic	is
//Indonesian	id
//Italian	it
//Japanese	ja
//Kannada	kn
//Korean	ko
//Latvian	lv
//Lithuanian	lt
//Malay	ms
//Malayalam	ml
//Marathi	mr
//Norwegian	no
//Polish	pl
//Portuguese (Portugal)	pt-PT
//Romanian	ro
//Russian	ru
//Serbian	sr
//Chinese (PRC)	zh-CN
//Slovak	sk
//Slovenian	sl
//Spanish	es
//Swahili	sw
//Swedish	sv
//Tamil	ta
//Telugu	te
//Thai	th
//Chinese (Taiwan)	zh-TW
//Turkish	tr
//Urdu	ur
//Ukrainian	uk
//Vietnamese	vi
//Welsh	cy
class TranslateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'translate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';


    private $client;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->client = new Client();
        parent::__construct();
    }

    public function handle()
    {
        //获取文件
        $from_file = file_get_contents(resource_path('lang/en.json'));
        $from_file = json_decode($from_file, true);
        $to_files = [
            'ar',
            'zh-TW',
            'jpn',
            'fr',
            'de',
            'ru'
        ];

        foreach ($to_files as $v) {
            $to_file = [];
            if (file_exists(resource_path("lang/{$v}.json"))) {
                $to_file = file_get_contents(resource_path("lang/{$v}.json"));
                $to_file = json_decode($to_file, true);
            }

            try {
                foreach ($from_file as $fk => $fv) {
                    if (!isset($to_file[$fk])) {
                        $to_file[$fk] = $this->translate($fv, $v);
                    }
                    usleep(1000);
                }

                file_put_contents(resource_path("lang/{$v}.json"), json_encode($to_file, JSON_UNESCAPED_UNICODE));
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        }

        return Command::SUCCESS;
    }

    //tl和q参数分别对应是目标语言和要翻译的内容
    public function translate($word, $en = 'en')
    {
        if (empty($word)) return '';
        $word = htmlspecialchars($word, ENT_NOQUOTES);
        $url = 'http://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl=' . $en . '&q=';
        $url .= urlencode($word);
        $rsp = $this->client->get($url);
        $rsp = json_decode($rsp->getBody(), true);
        return $rsp[0][0][0] ?? "";
    }
}
